package com.xintonghua.bussiness.ui.user;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.xintonghua.base.widget.ShowAllListView;
import com.xintonghua.bussiness.R;
import com.xintonghua.bussiness.adapter.EmployeeAdapter;
import com.xintonghua.bussiness.adapter.TaskManageAdapter;
import com.xintonghua.bussiness.api.manager.AuthManager;
import com.xintonghua.bussiness.base.BaseActivity;
import com.xintonghua.bussiness.bean.StaffManagerBean;
import com.xintonghua.bussiness.bean.TaskBean;
import com.xintonghua.bussiness.bean.TaskManageResponse;
import com.xintonghua.bussiness.ui.user.manage.SelectEmployeeActivity;
import com.xintonghua.bussiness.util.AbstractBaseAdapter;
import com.xintonghua.bussiness.util.JumpUtils;
import com.xintonghua.bussiness.util.MyState;
import com.xintonghua.bussiness.util.MyUtils;
import com.xintonghua.bussiness.util.ViewHolder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class DianZhangTaskManageActivity extends BaseActivity {
    @BindView(R.id.tv_all_task)
    TextView tvAllTask;
    @BindView(R.id.tv_now_task)
    TextView tvNowTask;
    @BindView(R.id.tv_complete)
    CheckBox tvComplete;
    @BindView(R.id.lv_complete)
    ShowAllListView lvComplete;
    @BindView(R.id.tv_uncomplete)
    CheckBox tvUncomplete;
    @BindView(R.id.lv_uncomplete)
    ShowAllListView lvUncomplete;
    BaseAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dian_zhang_task_manage);
        ButterKnife.bind(this);
        initUI();
    }

    @Override
    public void initUI() {
        super.initUI();
        onSimpleActionBar();
        setSimpleActionBar("目标管理", "分配目标", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openActivity(SelectEmployeeActivity.class);
            }
        });

        httpCent.staffManager(-1, this, MyState.requestTwo);

        httpCent.getTaskRecord(""+AuthManager.geteAuth().getId(),this, MyState.requestOne);
//        httpCent.getTaskPerMonthList(this, MyState.requestTwo);
        tvComplete.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                lvComplete.setVisibility(isChecked ? View.VISIBLE : View.GONE);
            }
        });
        tvUncomplete.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                lvUncomplete.setVisibility(isChecked ? View.VISIBLE : View.GONE);
            }
        });
        lvComplete.setAdapter(adapter = new AbstractBaseAdapter<StaffManagerBean>(beanList) {
            @Override
            public View getAdapterViewAtPosition(int position, View convertView, ViewGroup parent) {

                if (convertView == null) {
                    convertView = LayoutInflater.from(DianZhangTaskManageActivity.this).inflate(R.layout.adapter_item_meirongtest_history_record, null);
                }
                StaffManagerBean bean = getItem(position);
                TextView tv_type = ViewHolder.get(convertView, R.id.tv_type);
                tv_type.setText("" + bean.getUserName());
                return convertView;
            }
        });
        lvComplete.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                HashMap<String, String> map = new HashMap<>();
                map.put("id", "" + beanList.get(i).getId());
                JumpUtils.jumpto(DianZhangTaskManageActivity.this, TaskManageActivity.class, map);
            }
        });
    }

    List<StaffManagerBean> beanList=new ArrayList<>();

    @Override
    public void dataBack(Object data, int order) {
        super.dataBack(data, order);
        switch (order) {
            case MyState.requestOne:
                TaskBean taskBean = JSONObject.parseObject((String) data, TaskBean.class);
                tvAllTask.setText(taskBean.getTotal() + "");
                tvNowTask.setText("当前完成  " + taskBean.getTotal());
                break;
            case MyState.requestTwo:
                List<StaffManagerBean> a = JSONObject.parseArray((String) data, StaffManagerBean.class);
                if (MyUtils.isNullOrEmpty(a)) {
                    beanList.clear();
                    adapter.notifyDataSetChanged();
                } else {
                    beanList.clear();
                    beanList.addAll(a);
                    adapter.notifyDataSetChanged();
                }
                break;
        }
    }


}